<?php
class Controller_App_User extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/ucenter";
        parent::__construct($req, $res);
    }

    // 个人账户
    public function action_money()
    {
        $account = App_Account::account($this->_user);
        $this->template->set('account', $account);
        $this->template->set_filename('smarty:app/user_money');

        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的账户', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 个人信息修改
    public function action_edit($status = null)
    {
        $user  = User::currentUser();
        if(! empty($this->_post)){
            $mobile    = Arr::get($this->_post, 'usermobile');
            $code      = Arr::get($this->_post, 'code');
            if(!empty($code))
            {
                $guest = new User_V2_Guest();
                if (! $guest->validMobileCode($mobile, $code))
                    $this->request->redirect('/user/edit/1');
            }
            
            $arr  = array();
            $arr['userUid']    = $user->getUserUid();
            $arr['userName']   = Arr::get($this->_post, 'username');
            $arr['mobile']     = Arr::get($this->_post, 'usermobile');
            $arr['birthday']   = Arr::get($this->_post, 'userbirthday');
            $arr['comment']    = Arr::get($this->_post, 'usercomment');
            $arr['userGender'] = Arr::get($this->_post, 'sex');
            $arr['address']    = Arr::get($this->_post, 'useraddress');
            
            $user->userInfoEdit($arr);
            $this->request->redirect('/user');
        }
        
        if($status == 1)
        {
            $this->template->set('edit_status', 1);
        }else{
            $this->template->set('edit_status', 0);
        }
        
        $userinfo  = $user->getUserConfigInfo();

        $this->template->set('userinfo', $userinfo);
        
        $this->template->set_filename('smarty:app/user_edit');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '编辑资料', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 提现明细
    public function action_tixian_logs ($page = 1)
    {
        $limit  = 20;
        $page   = max($page, 1);
        $offset = ($page - 1) * $limit;

        $tixian = new App_Account_Payment($this->_uid);
        $logs   = $tixian->listRequests(-1, $offset, $limit);

        $this->template->set_filename('smarty:app/user_tixian_logs');
        $this->template->set('logs', $logs);
    }



    // 提现
    public function action_tixian()
    {
        if (empty($this->_post))
        {
            $this->template->set('moneyFen', App_Account::coin($this->_uid));
            $this->template->set('bankList', App_Account_Payment::bankList());
            $this->template->set_filename('smarty:app/user_tixian');
        }
        else
        {
            $tixian = new App_Account_Payment($this->_uid);
            $userName    = Arr::get($this->_post, 'userName');
            $accountName = Arr::get($this->_post, 'accountName');
            $bankName    = Arr::get($this->_post, 'bankName');
            $moneyYuan   = Arr::get($this->_post, 'money');
            $accountName = str_replace(' ', '', $accountName);

            $tixian->addRequest($accountName, $userName, $bankName, $moneyYuan);
            $this->request->redirect('/user/tixian_logs');
        }
    }

    // 我的晒单
    public function action_show ($uid = NULL)
    {
        $uid    = $uid > 0 ? $uid : $this->_uid;
        $show = new App_Show($uid);
        $iUser   = User::currentUser();
        $iBuyer  = $iUser->getUserInfo();
        
        $this->template->set('items', $show->user($uid));
        $this->template->set('userinfo', $iBuyer);
        $this->template->set_filename('smarty:app/show/user');
        $this->template->set('is_other_user', 0);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的晒单', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 我的粉丝
    public function action_fans($uid = NULL)
    {
        $this->template->set_filename('smarty:app/user_fans');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的粉丝', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);

        $uid    = $uid > 0 ? $uid : $this->_uid;
        $follow = new App_Follow($uid);
        $this->template->set('masterUid', $uid);
        $users  = $follow->setGroupType('user')->getFollowedUsers();
        $this->template->set('userList', $users);
    }

    // 我的关注
    public function action_attention($uid = NULL)
    {
        $this->template->set_filename('smarty:app/user_attention');

        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的关注', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);

        $uid    = $uid > 0 ? $uid : $this->_uid;
        $follow = new App_Follow($uid);
        $users  = $follow->setGroupType('user')->getFollowUsers();
        $this->template->set('masterUid', $uid);
        $this->template->set('userList', $users);
    }

    public function action_index ()
    {
        $this->action_ucenter();
    }

    // 修改密码
    public function action_resetpwd ()
    {
        if (! empty($this->_post))
        {
            $pwd = Arr::get($this->_post, 'password');
            if (! empty($pwd))
            {
                $this->_user->resetPassword($pwd);
                $this->template->set('status', 1);
            }
        }

        $this->template->set_filename('smarty:app/user_resetpwd');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '修改密码', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 退出
    public function action_logout ()
    {
        $this->set_no_cache();

        $this->_user->logout();
        $this->template->set_filename('smarty:app/user_logout');
    }

    // 个人中心
    public function action_ucenter ()
    {
        $this->set_no_cache();

        $this->template->set('orderStatsCount', App_Order::statsCount($this->_user));
        $this->template->set('address', App_Address::defaultAddr($this->_user));
        
        $title_json  = App_Util::buildClientTitleJson('back', 'none', '个人中心', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('block', 'ucenter');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
        
        $user  = User::currentUser();
        $userinfo  = $user->getUserConfigInfo();
        $this->template->set('userinfo', $userinfo);
        
        $account = App_Account::account($this->_user);
        $this->template->set('account', $account);
        
        $randomNum = rand(1, 10);
        $this->template->set('rand', $randomNum);
    }

    // 个人收货地址管理
    public function action_address ()
    {
        $addrList = App_Address::infoList($this->_user);
        $this->template->set_filename('smarty:app/address/list');
        $this->template->set('addressList', $addrList);
    }

    // 个人收藏记录
    public function action_likes ($type = 'goods')
    {
        //
        if ($type == 'goods')
        {
            $goodsItems = App_Goods::likeItemsByUid($this->_user->getUserUid(), 0, 10);
            $this->template->set('goodsItems', $goodsItems);
        }
        else
        {
            $shopsItems = App_Shop::likeItemsByUid($this->_user->getUserUid(), 0, 10);
            $this->template->set('shopItems', $shopsItems);
        }
        
        $this->template->set('type', $type);
        $this->template->set_filename('smarty:app/like/list');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的收藏', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 个人浏览记录
    public function action_history ()
    {
        //
        $goodsList = App_History::infoList($this->_user);
        $this->template->set_filename('smarty:app/history/list');
        $this->template->set('goodsItems', $goodsList);
    }

    // 个人消息
    public function action_messages ($page = 1)
    {
        $page = max(1, $page);
        $limit = 20;
        $offset = ($page - 1) * $limit;

        $message = new App_Message($this->_user->getUserUid());
        $messages = $message->getList($offset, $limit);
        $this->template->set('title', '我的消息');
        $this->template->set('messages', $messages);
        $this->template->set_filename('smarty:app/user_messages');
    }
    
    public function action_mobileSendCode3 ()
    {
        $mobile = Arr::get($this->_post, 'mobile');
        
        $isVerify = App_Register::hasVerify($mobile);
        if($isVerify){
            $ret    = array('status' => 1, 'mobile' => $mobile);
            $this->json_encode($ret);
            exit;
        }
        
        $new_user = new User_V2_Guest(); 
        $new_user->sendMobileCode($mobile);
        $ret    = array('status' => 0, 'mobile' => $mobile);

        $this->json_encode($ret);
    }
    
    public function action_mobileSendCode2 ()
    {
        $mobile = Arr::get($this->_post, 'mobile');
        
        $new_user = new User_V2_Guest(); 
        $new_user->sendMobileCode($mobile); 
        $ret    = array('status' => 0, 'mobile' => $mobile);

        $this->json_encode($ret);
    }

    public function before ()
    {
        parent::before();
        if ($this->_user->getUserUid() == 0)
        {
            echo "<script>window.location.assign('/login');</script>";
            exit;
        }
    }
}
